package com.atguigu.ggkt.vod.service.impl;


import com.alibaba.excel.EasyExcel;
import com.atguigu.ggkt.model.vod.Subject;
import com.atguigu.ggkt.swagger.exception.GgktException;
import com.atguigu.ggkt.vo.vod.SubjectEeVo;
import com.atguigu.ggkt.vod.listener.SubjectListener;
import com.atguigu.ggkt.vod.mapper.SubjectMapper;
import com.atguigu.ggkt.vod.service.SubjectService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2022-09-24
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {
    @Autowired
    private SubjectListener subjectListener;
    @Override
    public List<Subject> selectChild(Long id) {
        //1.查询id对应的子级subject
        LambdaQueryWrapper<Subject> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(id!=null,Subject::getParentId,id);
        List<Subject> subjectList = list(queryWrapper);
        List<Subject> subjects = subjectList.stream().map(item -> {
            Long subjectId = item.getId();
            boolean isChildren = isChildren(subjectId);
            if (isChildren) {
                item.setHasChildren(true);
            }
            return item;
        }).collect(Collectors.toList());
        return subjects;
    }

    /**
     * 导出数据
     * @param response
     */
    @Override
    public void exportData(HttpServletResponse response) {
        String fileName = null;
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            fileName = URLEncoder.encode("课程分类", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");
            List<Subject> subjectList = list();
            List<SubjectEeVo> subjectEeVoList = subjectList.stream().map(item -> {
                SubjectEeVo subjectEeVo = new SubjectEeVo();
                BeanUtils.copyProperties(item, subjectEeVo);
                return subjectEeVo;
            }).collect(Collectors.toList());
            EasyExcel.write(response.getOutputStream(),SubjectEeVo.class)
                    .sheet("课程分类")
                    .doWrite(subjectEeVoList);
        } catch (Exception e) {
            throw new GgktException(20001,"导出失败");
        }
    }

    /**
     * 导入课程分类
     * @param file
     */
    @Override
    public void importData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),SubjectEeVo.class
            ,subjectListener).sheet().doRead();
        } catch (IOException e) {
            throw new GgktException(20001,"导入失败");
        }
    }

    private boolean isChildren(Long subjectId) {
        LambdaQueryWrapper<Subject> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(subjectId!=null,Subject::getParentId,subjectId);
        int count = count(queryWrapper);
        return count>0;
    }
}
